//app.js
var util = require('/utils/util.js');
App({
    globalData: {
        userInfo: null,
        userVisitCount: null,
        token:null
    },

    onLaunch: function() {
        wx.showLoading({
            title: '数据加载中',
        })
        console.log("App onLaunch");
        var that = this;
        // 登录
        wx.login({
            success: res => {
                console.log("login success,res:", res);
                // 发送 res.code 到后台换取 openId, sessionKey, unionId

                //设置默认值
                var postData = {
                    code:res.code
                };
                var url = util.baseUrl + "/car/login";
                console.log("用户开始登陆");
                wx.request({
                    url: url,
                    data: postData,
                    method: 'GET',
                    header: {
                        // 'content-type': 'application/json',
                    },
                    success: function (res) {
                        wx.hideLoading();
                        //处理响应数据
                        res = res.data;
                        console.log("user响应res:{}", res);
                        if (res.code != 1) {
                            wx.showModal({
                                title: '提示',
                                content: res.msg,
                                showCancel: false
                            })
                        } else {
                            //todo 设置缓存
                            // console.info("login success,token:",res.data.);
                            wx.setStorageSync("token", res.data.token);
                            that.globalData.token = res.data.token; 
                            that.globalData.userVisitCount = res.data.userVisitCount;
                            console.log("用户登陆完成");
                        }  
                  
        
                        //判断是否有回调函数
                        if (that.loginReadyCallback) {
                            console.log("login Callback")
                            that.loginReadyCallback(res)
                        }

                        //执行回调
                        if (that.addUserVisitCallback) {
                            console.log("执行addUserVisitCallback");
                            that.addUserVisitCallback(res.data.userVisitCount)
                        }

                    },
                    fail: function (e) {
                        console.log("请求失败:", e);
                        wx.showModal({
                            title: '提示',
                            content: '服务器异常,请稍后再试',
                            showCancel: false
                        })
                    }
                });
            },
            fail: res => {
                console.log("login fail,res:", res);
            }
        });

        // 获取用户信息
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: res => {
                            console.log("getUserInfo Success,res:", res);
                            // 可以将 res 发送给后台解码出 unionId
                            that.globalData.userInfo = res.userInfo

                            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                            // 所以此处加入 callback 以防止这种情况
                            if (that.userInfoReadyCallback) {
                                console.log("getUserInfo Callback")
                                that.userInfoReadyCallback(res)
                            }
                            console.log("that.globalData.userInfo:", that.globalData.userInfo);
                        }
                    })
                }
            }
        });

       

    },



    addUserVisit: function() {
        //设置默认值
        var that = this;
        var postData = {

        };
        var url = util.baseUrl + "/car/addUserVisit";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            // data: postData,
            method: 'GET',
            header: {
                // 'content-type': 'application/json',
            },
            success: function(res) {
                //处理响应数据
                res = res.data;
                console.log("user响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '提示',
                        content: res.msg,
                        showCancel: false
                    })
                } else {
                    that.globalData.userVisitCount = res.data.userVisitCount;
                }

                //执行回调
                if (that.addUserVisitCallback) {
                    console.log("执行addUserVisitCallback");
                    that.addUserVisitCallback(res.data.userVisitCount)
                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
                wx.showModal({
                    title: '提示',
                    content: '服务器异常,请稍后再试',
                    showCancel: false
                })
            }
        })
    }
})